package game.test;

import entity.GameEntity;
import entity.Position;
import entity.map.FieldEntity;
import game.game.ControllerGameView;
import game.game.GameModel;

import java.util.List;

import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

public class GameView extends BasicGame
{
	protected Image _niv1;
	protected Image _wall;
	protected Image _perso;
	protected Image _obj;

	protected GameModel _model;

	protected ControllerGameView _controller;

	public final static int POSITION_PLAYER_ON_SCREEN_X = 4;
	public final static int POSITION_PLAYER_ON_SCREEN_Y = 4;

	public GameView(String title, GameModel pModel, ControllerGameView pController)
	{
		super( title );
		_model = pModel;
		_controller = pController;
	}

	@Override
	public void render( GameContainer arg0 , Graphics pGraphics ) throws SlickException
	{
		// Affichage du terrain
		for ( int i = 0; i < 10; i++ )
		{
			for ( int j = 0; j < 10; j++ )
			{
				drawRoom( i , j );
			}
		}
		// affichage du joueur
		_perso.draw( POSITION_PLAYER_ON_SCREEN_X * _perso.getWidth() , POSITION_PLAYER_ON_SCREEN_Y * _perso.getHeight() );
		// affichae des autres objets
		if ( _model.getPerso() != null )
		{
			List<GameEntity> listObj = _model.getGameObjects();
			for ( GameEntity obj : listObj )
			{
				if ( obj != _model.getPerso() )
				{
					int positionX = obj.getPosition().getX() - _model.getPerso().getPosition().getX() + POSITION_PLAYER_ON_SCREEN_X;
					int positionY = obj.getPosition().getY() - _model.getPerso().getPosition().getY() + POSITION_PLAYER_ON_SCREEN_Y;
					if ( positionX >= 0 && positionX < 10 && positionY >= 0 && positionY < 10 )
					{
						_obj.draw( positionX * _obj.getWidth() , positionY * _obj.getHeight() );
						//System.out.println( obj.getPosition() + ":" + _model.getPerso().getPosition() + " => " + positionX + "," + positionY );
					}
				}
			}
			//
			pGraphics.drawString( _model.getPerso().getPosition().toString() , 100 , 20 );
		}

	}

	public void drawRoom( int pScreenX , int pScreenY ) throws SlickException
	{
		if ( _model.getPerso() != null )
		{
			int positionX = pScreenX + _model.getPerso().getPosition().getX() - POSITION_PLAYER_ON_SCREEN_X;
			int positionY = pScreenY + _model.getPerso().getPosition().getY() - POSITION_PLAYER_ON_SCREEN_Y;
			FieldEntity fieldEntity = _model.getMap().getFieldEntity( new Position( positionX, positionY ) );

			if ( fieldEntity != null )
			{
				if ( fieldEntity.isBlock() )
				{
					_wall.draw( pScreenX * _wall.getWidth() , pScreenY * _wall.getHeight() );
				}
				else
				{
					_niv1.draw( pScreenX * _wall.getWidth() , pScreenY * _wall.getHeight() );
				}
			}
		}
	}

	@Override
	public void init( GameContainer arg0 ) throws SlickException
	{
		_niv1 = new Image( "data/herbe.png" );
		_wall = new Image( "data/wall3.bmp" );
		_perso = new Image( "data/perso.bmp" );
		_obj = new Image( "data/obj.bmp" );
	}

	@Override
	public void update( GameContainer gc , int arg1 ) throws SlickException
	{
		if ( _model.getPerso() != null )
		{
			Position persoPos = _model.getPerso().getPosition();
			Input input = gc.getInput();

			if ( input.isKeyPressed( Input.KEY_UP ) )
			{
				changePosition( persoPos.getX() , persoPos.getY() - 1 );
			}
			if ( input.isKeyPressed( Input.KEY_DOWN ) )
			{
				changePosition( persoPos.getX() , persoPos.getY() + 1 );
			}
			if ( input.isKeyPressed( Input.KEY_LEFT ) )
			{
				changePosition( persoPos.getX() - 1 , persoPos.getY() );
			}
			if ( input.isKeyPressed( Input.KEY_RIGHT ) )
			{
				changePosition( persoPos.getX() + 1 , persoPos.getY() );
			}
		}
	}

	private void changePosition( int pNewX , int pNewY )
	{
		_controller.changePosition( pNewX , pNewY );
	}

}
